﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Entities;
using BLL;

namespace Stationery.UC
{
    public partial class UC_NewsList : System.Web.UI.UserControl
    {
        BLL_News bll_News = new BLL_News();
        Entities.User obj_User = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            obj_User = (Entities.User)Session[Utils.SESSION_LOGIN];
            if (obj_User == null)
            {
                Response.Redirect(UtilsURL.home);
            }

            if (!IsPostBack)
                BindDataASPxGridView();
            else
            {
                if (Session["data"] == null)
                {
                    return;
                }
                else
                {
                    DataTable table = (DataTable)Session["data"];
                    DataView dv = table.DefaultView;
                    ASPxGridViewNewsList.DataSource = dv;
                    ASPxGridViewNewsList.DataBind();
                }
            }
        }

        #region Bind Data into Gridview
        protected void BindDataASPxGridView()
        {
            DataTable dt = new DataTable();
            try
            {
                dt = bll_News.Get_News_All();
                if (Session["data"] == null)
                {                    
                    if (dt == null)
                        return;
                    else
                        Session["data"] = dt;
                }
                else
                {
                    dt = (DataTable)Session["data"];
                }
                DataView dv = dt.DefaultView;
                ASPxGridViewNewsList.DataSource = dv;
                ASPxGridViewNewsList.DataBind();

            }
            catch (Exception ex)
            {
                Session["data"] = null;
                dt = null;
            }
        }
        #endregion
        #region Event click
        protected void ASPxButtonAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect(UtilsURL.news_add, true);
        }

        protected void ImageButtonEdit_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton imageButton = sender as ImageButton;
            Int32 intID = Convert.ToInt32(imageButton.CommandArgument);
            Response.Redirect(String.Format(UtilsURL.news_edit, intID));
        }

        protected void ImageButtonDelete_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton imageButton = sender as ImageButton;
            Int32 intID = Convert.ToInt32(imageButton.CommandArgument);

            bll_News.DeleteNews(intID);
            Session["data"] = null;           
            Response.Redirect(Request.RawUrl);
        }

        #endregion
    }
}